package com.tongcheng.Controller;

import com.tongcheng.ResultDate;
import com.tongcheng.commons.Contants;
import com.tongcheng.pojo.*;
import com.tongcheng.service.Activitysevice;
import com.tongcheng.utils.ConversionDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activity")
@Api(value = "活动api",tags = "活动")
public class ActivityController {
    @Autowired
    private Activitysevice activitysevice;
    @ApiOperation(value = "活动添加",notes = "根据提供信息添加活动", response = ResultDate.class)
    @RequestMapping(value = "/addActivity",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate addActivity(@RequestBody Map map){
        map.put("activity_end_time", ConversionDateUtils.conversionDateUtils(map.get("activity_end_time").toString()));
        map.put("activity_start_time",ConversionDateUtils.conversionDateUtils(map.get("activity_start_time").toString()));
        int result1=activitysevice.addActivity(map);
        ResultDate resultDate=new ResultDate();
        if (result1>0){
            int result3=activitysevice.addActivity_coupons(map);
            if (result3>0){
                resultDate.setCode(Contants.SUCCESS);
                resultDate.setMsg("添加成功");

            }else {
                resultDate.setCode(Contants.FAILURE);
                resultDate.setMsg("失败");
            }
        }else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败");
        }
        return resultDate;
    }
    @ApiOperation(value = "活动删除",notes = "根据id删除活动", response = ResultDate.class)
    @RequestMapping(value = "/delActivity",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate delActivity(Integer activity_id){

        System.out.println("activity_id----------"+activity_id);
        int result=activitysevice.delActivity(activity_id);
        ResultDate resultDate=new ResultDate();

        if (result>0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("删除成功");
//
        }else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败");
        }
        return resultDate;
    }
    @ApiOperation(value = "活动修改",notes = "根据提供信息修改活动", response = ResultDate.class)
    @PostMapping(value = "/updateActivity", produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate updateActivity(@RequestBody Map map){
        map.put("activity_end_time", ConversionDateUtils.conversionDateUtils(map.get("activity_end_time").toString()));
        map.put("activity_start_time",ConversionDateUtils.conversionDateUtils(map.get("activity_start_time").toString()));
        int result=activitysevice.updateActivity(map);
        ResultDate resultDate=new ResultDate();
        if (result>0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("修改成功");

        }else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败");
        }
        return resultDate;
    }
    @ApiOperation(value = "活动查询",notes = "根据id查询活动", response = ResultDate.class)
    @GetMapping(value = "/queryActivity",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate queryActivity( Integer activity_id){
        List<Map> mapList=activitysevice.queryActivity(activity_id);
        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        }
        return resultDate;
    }
    @ApiOperation(value = "所有活动查询",notes = "查询活动", response = ResultDate.class)
    @GetMapping(value = "/queryAllActivity",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate queryAllActivity( ){
        List<Map> mapList=activitysevice.queryAllActivity();
        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        }
        return resultDate;

    }
//    @ApiOperation(value = "活动分享设置添加",notes = "活动分享设置添加", response = ResultDate.class)
//    @PostMapping(value = "/addActivityshare",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ResponseBody
//    public ResultDate addActivity_share(@RequestBody Map map ){
//        map.put("activity_end_time", ConversionDateUtils.conversionDateUtils(map.get("activity_end_time").toString()));
//        map.put("activity_start_time",ConversionDateUtils.conversionDateUtils(map.get("activity_start_time").toString()));
//        int result=activitysevice.addActivity_share(map);
//        ResultDate resultDate=new ResultDate();
//        if(result>0){
//            resultDate.setCode(Contants.SUCCESS);
//            resultDate.setMsg("添加成功");
//        }else {
//            resultDate.setCode(Contants.FAILURE);
//            resultDate.setMsg("添加失败");
//        }
//
//        return resultDate;
//    }
}
